// pages/toPhoto/toPhoto.js
const app = getApp()
var base_url = app.globalData.url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // display:true,
    ishidden:'hidden',
    isdisplay:'none',
    imgWidth: 0,
    imgHeight: 0,
    imgTop: 0,
    imgLeft: 0,
    isCroper: false,
    mengban:"block",
    h1:true,
    src:'../../../../../../../images/icons/paii.png',
    display:'true',
    mengban2:"none",
    td1:"block",
    banh:"block"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this
    wx.getSystemInfo({

      success: function (res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let height = clientHeight * ratio;
        self.setData({
          height: clientHeight
        });
      }
    });
    self.device = app.globalData.myDevice
    self.deviceRatio = self.device.windowWidth / 750
    self.imgViewHeight = self.device.windowHeight - 160 * self.deviceRatio
    self.setData({
      imgViewHeight: self.imgViewHeight,
      // tempCanvasHeight: self.imgViewHeight,
      page: 'mainPage'
    })
    self.setData({
      imgWidth: wx.getSystemInfoSync().windowWidth,
      imgHeight: wx.getSystemInfoSync().windowHeight*0.7
    })
    // wx.showModal({
    //   title: '拍照要求',
    //   content: '1.纸张平整放置再桌面拍摄\r\n2.拍摄角度再垂直拍摄角度的左右30°以内\r\n3.需要多种光照条件来拍摄来适应更多的环境',
    //   success: function (res) {
    //     if (res.confirm) {
    //       console.log('弹框后点确定')
    //     } else {
    //       console.log('弹框后点取消')
    //     }
    //   }
    // })
    var minCutL = Math.max(0, this.data.imgLeft)
    var minCutT = Math.max(0, this.data.imgTop)
    this.setData({
      isCroper: true,
      cutW: 150,
      cutH: 100,
      cutL: minCutL,
      cutT: minCutT
    })
    var that = this
    wx.getStorage({
      key: 'auth',
      success: function (res) {
        that.setData({
          auth:res.data
        })
      },
    })

    wx.getStorage({
      key: 'student_id',
      success: function (res) {
        that.setData({
          student_id: res.data
        })
      },
    })

    wx.getStorage({
      key: 'teacher_id',
      success: function (res) {
        that.setData({
          teacher_id: res.data
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  takePhoto(){
    var that = this
    const ctx = wx.createCameraContext();
    ctx.takePhoto({
      quality:'high',
      success:(res)=>{
        console.log(res.tempImagePath)
        this.setData({
          src:res.tempImagePath,
          display:false,
          td1:"none"
        })
      }
    })
    that.setData({
      ishidden:'none'
    })
  },
  chooseimage: function () {
    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        _this.setData({
          tempFilePaths: res.tempFilePaths,
          display: true
        })
      }
    })
  },
  backBtn:function(){
    var that = this;
    that.setData({
      display:"none",
      td1: "block"
    })
    // wx.navigateBack({
    //   delta:1
    // })td1:"block"
  },
  uploadBtn:function(){
    var that = this;
    that.setData({
      banh:"none",
      isdisplay:'block',
      mengban2: "block",
      
    })

    // wx.getFileSystemManager().readFile({
    //   filePath: this.data.src, //选择图片返回的相对路径
    //   encoding: 'base64', //编码格式
    //   success: res => { //成功的回调
    //     // console.log('data:image/png;base64,' + res.data)
    //     wx.request({
    //       url: base_url + '/uploadDisPic/',
    //       method: 'POST',
    //       data: {
    //         auth: that.data.auth,
    //         teacher_id: that.data.teacher_id,
    //         student_id: that.data.student_id,
    //         file: res.data
    //       },
    //       header: {
    //         "Content-Type": "application/x-www-form-urlencoded"
    //       },
    //       success: function (res) {
    //         if (res.data['flag'] == true) {
    //           wx.setStorage({
    //             key: 'path',
    //             data: res.data['path'],
    //             success: function () {
    //               that.setData({
    //                 isdisplay: 'none'
    //               })
    //             }
    //           })
    //           wx.setStorage({
    //             key: 'markPic',
    //             data: res.data['markPic'],
    //             success: function () {
    //             }
    //           })
    //           wx.setStorage({
    //             key: 'sysPicLst',
    //             data: res.data['sysPicLst'],
    //             success: function () {
    //             }
    //           })
    //           wx.redirectTo({
    //             url: '../allResult/allResult'

    //           })
    //         }
    //       },
    //       fail:function(res){
    //         console.log(res)
    //       }
    //     })
    //   }
    // })

    wx.uploadFile({
      url: base_url+"/uploadDisPic/",
      filePath: this.data.src,
      name: 'file',
      formData: {
        auth:that.data.auth,
        teacher_id:that.data.teacher_id,
        student_id:that.data.student_id
      },
      success: function (res) {
        console.log(res)
        var jsonObj = JSON.parse(res.data)
        if (jsonObj['flag'] == true){
          
          wx.setStorage({
            key: 'path',
            data: jsonObj['path'],
            success: function () {
              that.setData({
                isdisplay: 'none',
                mengban2: "none"
              })
            }
          })
          wx.setStorage({
            key: 'markPic',
            data: jsonObj['markPic'],
            success: function () {
            }
          })
          wx.setStorage({
            key: 'cropPicsPath',
            data: jsonObj['cropPicsPath'],
            success: function () {
            }
          })
          wx.setStorage({
            key: 'strData',
            data: jsonObj['strData'],
            success: function () {
            }
          })
          wx.redirectTo({
            url: '../allResult/allResult'

          })
          
        }else{
          wx.showModal({
            title:"提示",
            content:"检测失败",
            success:function(res){
              if(res.confirm){
                wx.redirectTo({
                  url: '../toPhoto/toPhoto'
                })
              }else{
                wx.redirectTo({
                  url: '../toPhoto/toPhoto'
                })
              }
            }
          })
          
        }
        
      },fail:function(res){

      }
    })
  },
  croperStart(e) {
    this.croperX = e.touches[0].clientX
    this.croperY = e.touches[0].clientY
  },
  croperMove(e) {
    var self = this
    var dragLengthX = (e.touches[0].clientX - self.croperX)
    
    var dragLengthY = (e.touches[0].clientY - self.croperY)
    var minCutL = Math.max(0, self.data.imgLeft)
    var minCutT = Math.max(0, self.data.imgTop)
    var maxCutL = Math.min(750 * self.deviceRatio - self.data.cutW, self.data.imgLeft + self.data.imgWidth - self.data.cutW)
    console.log(dragLengthX)
    var maxCutT = Math.min(self.imgViewHeight - self.data.cutH, self.data.imgTop + self.data.imgHeight - self.data.cutH)
    var newCutL = self.data.cutL + dragLengthX
    var newCutT = self.data.cutT + dragLengthY
    
    if (newCutL < minCutL) newCutL = minCutL
    if (newCutL > maxCutL) newCutL = maxCutL
    if (newCutT < minCutT) newCutT = minCutT
    if (newCutT > maxCutT) newCutT = maxCutT
    
    // console.log(newCutL)
    this.setData({
      cutL: newCutL,
      cutT: newCutT,
    })
    self.croperX = e.touches[0].clientX
    self.croperY = e.touches[0].clientY
  },
  dragPointStart(e) {
    var self = this
    self.dragStartX = e.touches[0].clientX
    self.dragStartY = e.touches[0].clientY
    self.initDragCutW = self.data.cutW
    self.initDragCutH = self.data.cutH
  },
  dragPointMove(e) {
    var self = this
    var maxDragX = Math.min(750 * self.deviceRatio, self.data.imgLeft + self.data.imgWidth)
    var maxDragY = Math.min(self.imgViewHeight, self.data.imgTop + self.data.imgHeight)
    var dragMoveX = Math.min(e.touches[0].clientX, maxDragX),
      dragMoveY = Math.min(e.touches[0].clientY, maxDragY);
    var dragLengthX = dragMoveX - self.dragStartX
    var dragLengthY = dragMoveY - self.dragStartY

    if (dragLengthX + self.initDragCutW >= 0 && dragLengthY + self.initDragCutH >= 0) {
      
      self.setData({
        cutW: self.initDragCutW + dragLengthX,
        cutH: self.initDragCutH + dragLengthY
      })
    } else {
      return
    }
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  sure:function(){
    this.setData({
      mengban:"none",
      h1:false
    })
  }
})

  